const path = require('path');
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin')
const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const autoprefixer = require('autoprefixer')
const px2rem = require('postcss-px2rem')
const publicPath = '/'
//const paths = require()
const paths = require('./config/paths')
// Do this as the first thing so that any code reading it knows the right env.
process.env.BABEL_ENV = 'development';
process.env.NODE_ENV = 'development';
process.env.STATIC_ENV = 'development';
let WEBPACK_ENV = process.env.WEBPACK_ENV  || 'dev'

console.log(WEBPACK_ENV)
function resolve(dir){
	return path.join(__dirname,dir)
}
module.exports={


	entry:[
		'react-hot-loader/patch',
		'babel-polyfill',
		paths.appIndexJs
		//require('./config/polyfills')
	],
	output:{
		// path:path.resolve(__dirname,'dist'),
		// publicPath: WEBPACK_ENV === 'dev'? '/dist/':'//s',
		// filename:'js/app.js'
		pathinfo:true,
		filename:'static/js/bundle.js',
		publicPath
		//chunFilename:'static/js/[name].chunk.js'
	},
	resolve:{
		alias:{
			// page: path.resolve(__dirname,'src/page'),
			// component:path.resolve(__dirname,'src/component'),
			// util:path.resolve(__dirname,'src/util'),
			// service:path.resolve(__dirname,'src/service')
			'babel-runtime':path.dirname(require.resolve('babel-runtime/package.json')),
			'react-native':'react-native-web',
			'@':resolve('src')

		}
	},
	module:{
		rules:[
			// {
			// 	test:/\.jsx$/,
			// 	exclude:/(node_modules)/,
			// 	use:{
			// 		loader:'babel-loader',
			// 		options:{
			// 			presets:['env','react']
			// 		}
			// 	}
			// },
			// {
			// 	test:/\.css$/,
			// 	use:ExtractTextPlugin.extract({
			// 		fallback:'style-loader',
			// 		use:'css-loader'
			// 	})
			// },
			// {
			// 	test:/\.scss$/,
			// 	use:ExtractTextPlugin.extract({
			// 		fallback:'style-loader',
			// 		use:['css-loader','sass-loader']
			// 	})
			// },
			// {
			// 	test:/\.(png|jpg|gif)$/,
			// 	use:[{
			// 		loader:'url-loader',
			// 		options:{
			// 			limit:8192,
			// 			name:'resource/[name].[ext]'
			// 		}
			// 	}]
			// },

			{
				oneOf:[
					{
						test:[/\.bmp$/,/\.gif$/,/\.jpe?g$/,/\.png$/],
						loader:require.resolve('url-loader'),
						options:{
							limit:10000,
							name:'static/media/[name].[hash:8].[ext]'
						}
					},
					{
						test:/\.(js|jsx|mjs)$/,
						exclude:/(node_modules)/,
						use:[
							{
								loader:require.resolve("babel-loader"),
								options:{
									babelrc:true,
									presets:[require.resolve("babel-preset-react-app")],
									cacheDirectory:true,
									plugins:["transform-decorators-legacy"],
								}
							}
						]
					},
					{
						test:/\.(css|less)$/,
						use:[
							require.resolve('style-loader'),
							//px2rem({remUnit:75}),
							{
								loader:require.resolve('css-loader'),
								options:{
									importLoaders:1,
								}
							},
							{
								// // https://github.com/facebookincubator/create-react-app/issues/2677
								// postcss 平台提供 自适应的css插件 autoprefixer ,自适应单位插件 px2rem
								//loader:require.resolve('postcss-loader'),
								loader:'postcss-loader',
								options:{
									ident:'postcss',
									plugins:()=>[
										require('postcss-flexbugs-fixes'),
										require('postcss-px2rem')({remUnit:36.5}),
										autoprefixer({
											browsers:[
												'>1%',
												'last 2 versions',
												'Firefox ESR',
												'not ie < 9'
											],
											flexbox:'no-2009'
										}),
										//px2rem({remUnit:75})
									]
								}
							},
							{
								loader:require.resolve('less-loader')
							}
							//require('postcss-px2rem')({remUnit:75})
						]
					},
					{
						exclude:[/\.js$/,/\.html$/,/\.json$/],
						loader:require.resolve('file-loader'),
						options:{
							name:'static/media/[name].[hash:8].[ext]'
						}
					}
				]
			}
		]
	},
	// postcss: function() {
  //   return [px2rem({remUnit: 75})];
  // },
	plugins:[
		// new HtmlWebpackPlugin(
		//  {
		//  	template:'./src/index.html',
		//  	favicon:'./favicon.ico'

		//  }

		// ),
		// new ExtractTextPlugin('css/[name].css'),
		// new webpack.optimize.CommonsChunkPlugin({
		// 	name:'common',
		// 	filename:'js/base.js'
		// })

		// Generate an index.html and inject script
		new HtmlWebpackPlugin({
			inject:true,
			template:paths.appHtml,
			favicon:'./public/favicon.ico'
		}),
		new webpack.NamedModulesPlugin(),
		// Global variable
		new webpack.DefinePlugin({
			AUTHOR:'Andy',
			VERSION:'1.0'
		}),
		new webpack.HotModuleReplacementPlugin(),

		// See https://github.com/facebookincubator/create-react-app/issues/240
		new CaseSensitivePathsPlugin(),
		// npm install a module, not need to restart developement server
		// See https://github.com/facebookincubator/create-react-app/issues/186
		new WatchMissingNodeModulesPlugin(paths.appNodeModules),
		// Ignore locale when reqire  moment.js
		new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)

	],
	
	node:{
		dgram:'empty',
		fs:'empty',
		net:'empty',
		tls:'empty',
		child_process:'empty'
	},
	devServer:{
		port:8088
		/*
		historyApiFallback:{
			index:'/dist/index.html'
		},
		proxy:{
			'/manage':{
				target:'http://admintest.happymmall.com',
				changeOrigin:true
			},
			'/user/logout.do':{
				target:'http://admintest.happymmall.com',
				changeOrigin:true
			}
		}
		*/
	}
}
;